﻿using System;
using System.Collections.Generic;
using System.Linq;
using BostonAzureSite.Models;
//using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace BostonAzureSite.Models
{   
    /// <summary>
    /// This class will serve as the data access layer for all Azure Events.
    /// </summary>
    public class BostonAzureEventRepository
    {
        // Failed db access can throw:
        // - InvalidOperationException for thread pool exhaustion or timeout 
        //   (network not fully available to access db from app server)
        // --^-- probably just a consideration for testing on disconnected laptop

        static readonly TimeSpan OneDayTimeSpan = new TimeSpan(1, 0, 0, 0);

        /// <summary>
        /// Returns an event requested by a user.
        /// </summary>
        /// <param name="eventId">Unique Event Identifier</param>
        /// <returns>Requested Event</returns>
        public Event FindSpecificEvent(int eventId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the next event for Boston Azure.
        /// </summary>
        /// <returns>An Event</returns>
        public Event FindNextEvent()
        {
            using (var dbContext = new BostonAzureDataContext())
            {   
                Event e = null;

                try
                {
                    // Granularity of "today" is fine; need to normalize comparison for UTC
                    // NOTE: changed meaning of start_utc to be start_local (TODO: change names)
                    // Business Rule: "upcoming" should include events that happen today
                    e = (Event)(from ev in dbContext.Events
                                // TODO: ideally, do I just want the "date" part of the DateTime?
                                where ev.start_utc >= DateTime.Today.Subtract(OneDayTimeSpan)
                                orderby ev.start_utc
                                select ev).First();
                }
                catch (Exception ex)
                {
//                    Logger.Write(new LoggingException("Error retrieving next event.", ex));
                }

                return e;
            }
           
        }

        /// <summary>
        /// Returns a list of upcoming events for Boston Azure Group.
        /// </summary>
        /// <returns>List of upcoming Events</returns>
        public List<Event> FindUpcomingEvents()
        {
            using (BostonAzureDataContext dbContext = new BostonAzureDataContext())
            {   
                List<Event> eventList = new List<Event>();

                //we don't need this for readonly queries
                dbContext.ObjectTrackingEnabled = false;

                try
                {
                    // Granularity of "today" is fine; need to normalize comparison for UTC
                    // NOTE: changed meaning of start_utc to be start_local (TODO: change names)
                    // Business Rule: "upcoming" should include events that happen today
                    eventList = (from ev in dbContext.Events
                                         // TODO: ideally, do I just want the "date" part of the DateTime?
                                         where ev.start_utc >= DateTime.Today.Subtract(OneDayTimeSpan)
                                         orderby ev.start_utc
                                         select ev).ToList();
                }
                catch (Exception ex)
                {
//                    Logger.Write(new LoggingException("Error retrieving upcoming events.", ex));
                }

                return eventList;
            }

        }

        /// <summary>
        /// This method will find all the events for the Boston Azure Group.
        /// </summary>
        /// <returns>List of all Events.</returns>
        public List<Event> FindAllEvents()
        {
            using (BostonAzureDataContext dbContext = new BostonAzureDataContext())
            {
                List<Event> eventList = new List<Event>();

                //we don't need this for readonly queries
                dbContext.ObjectTrackingEnabled = false;

                try
                {
                    eventList = (from ev in dbContext.Events
                                 orderby ev.start_utc
                                 select ev).ToList();
                }
                catch (Exception ex)
                {
//                    Logger.Write(new LoggingException("Error retrieving all events.", ex));
                }

                return eventList;
            }
        }
    }
}


